﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Besture.Bll;
using Besture.Bll.UserBase;
using Besture.Common;
using Besture.Common.Definition;
using Besture.Common.Helper;
using Besture.Model;

namespace Besture.UI.Common
{
    public class UserPage :System.Web.UI.Page
    {
        ///<summary>
        /// Page load event
        ///</summary>
        ///<remarks></remarks>
        protected override void OnLoad(System.EventArgs e)
        {
            GetLogin();
            base.OnLoad(e);
        }
        /// <summary>
        /// 获取Session里的UserModel
        /// </summary>
        /// <returns></returns>
        public static UserBaseModel GetUserLoginModel()
        {
            return HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] as UserBaseModel;
        }

        /// <summary>
        /// 获取登录用户ID
        /// </summary>
        /// <returns></returns>
        public  static int GetUserLoginID()
        {
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] == null)
            {
                return 0;
            }
            return (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] as UserBaseModel).ID;
        }

        /// <summary>
        /// 登录判断
        /// </summary>
        public bool GetLogin()
        {
            //判断是否登录
            if (HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] == null)
            {
                //默认返回普通用户登录页面
                //var returnLink = WebLoginUrl();
                //if (!String.IsNullOrEmpty(CookieHelper.GetCookie("LoginUsetType")))
                //{

                //}
                //HttpContext.Current.Response.Write("<script language='JavaScript'>");
                //HttpContext.Current.Response.Write(" alert('您没有登录或登录已过期，请重新登录！');");
                //HttpContext.Current.Response.Write(" location.href='" + returnLink + "';");
                //HttpContext.Current.Response.Write("</script>");
                Response.Redirect("/Default.aspx");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 前台登录页面
        /// </summary>
        /// <returns></returns>
        public static string WebLoginUrl()
        {
            return string.Format(@"{0}?returnUrl={1}", AppSettings.WEB_ADMIN_LOGIN, HttpContext.Current.Server.UrlEncode(HttpContext.Current.Request.Url.PathAndQuery));
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        protected UserBaseModel LoginUserInfo
        {
            get { return HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL] == null ? null : (UserBaseModel)HttpContext.Current.Session[Constants.SESSION_WEB_USER_MODEL]; }
        }


        /// <summary>
        /// 未读站内信数量
        /// </summary>
        /// <returns></returns>
        //public static string GetStationLettersCount()
        //{
        //    var dt = new StationLettersDac().GetCountByUserId(GetUserLoginID());
        //    if (dt.Rows.Count==0)
        //    {
        //        return "";
        //    }
        //    else
        //    {
        //        return "("+dt.Rows.Count+")";
        //    }
        //}

    }
}